package db;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Singleton instance for Hibernate session.
 */
public class DatabaseAccess {

	private static SessionFactory factory;
	private static Session session;

	private static DatabaseAccess instance;

	private DatabaseAccess() {
		factory = new Configuration().configure(
		                              "db/resources/hibernate.cfg.xml")
		                              .buildSessionFactory();
		session = factory.openSession();
	}

	public static Session getInstance() {
		if (instance == null) {
			instance = new DatabaseAccess();
		}

		if (session.isConnected()) {
			return session;
		} else {
			return session = factory.openSession();
		}
	}

}
